package com.tangula.web.kashine.business;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.tangula.web.form.DefaultPagingBody;
import com.tangula.web.kashine.entity.LoadProductOfOrgStoreForm;
import com.tangula.web.kashine.entity.OrgProductForm;
import com.tangula.web.kashine.entity.ProductEntity;
import com.tangula.web.kashine.entity.ProductForm;
import com.tangula.web.kashine.entity.StorePageableForm;
import com.tangula.web.kashine.repository.ProductEntityRepository;
import com.tangula.web.kashine.repository.StoreRepository;
@Service
public class ProductBusinessImpl implements ProductBusiness{
	@Autowired
	ProductEntityRepository productEntityRepository;
	@Autowired
	StoreRepository storeRepository;
	@Override
	public DefaultPagingBody<OrgProductForm> loadProductsOfOrgStore(LoadProductOfOrgStoreForm form) {
		if(form!=null) {
			List<OrgProductForm> pop = new ArrayList<>();
			PageRequest pr = PageRequest.of(form.getIndex()-1, form.getSize(), Sort.by(new Sort.Order(Direction.ASC, "id")));
			List<ProductEntity> proLists = productEntityRepository.findByOwnerStore(form.getCondition(), pr);
			for(ProductEntity list : proLists) {
				OrgProductForm opf = new OrgProductForm(list.getId(), list.getProductName(), list.getProductDescription(), list.getProductPrice()+"", list.getProductImage());
				pop.add(opf);
			}
			DefaultPagingBody<OrgProductForm> d = new DefaultPagingBody<>(form.getIndex(), form.getSize(), pop.size(), pop);
			return d;
		}else {
			return null;
		}
	}
	@Override
	public ProductEntity removeProductsFromOrgStore(ProductEntity product) {
		if(product!=null) {
		product.setOwnerStore(null);
		return storeRepository.saveAndFlush(product);
		}else{
			return null;
		}
	}
	@Override
	public ProductEntity addOrgStoreProduct(ProductEntity product) {
		if(product!=null) {
		return storeRepository.saveAndFlush(product);
		}else {
			return null;
		}
	}
	@Override
	public DefaultPagingBody<ProductEntity> loadProductInStore(StorePageableForm storePag) {
		if(storePag!=null) {
			DefaultPagingBody<ProductEntity> d = new DefaultPagingBody<>(storePag.getIndex(),
					storePag.getSize(), 
					productEntityRepository.findByOwnerStore(storePag.getCondition()).size(), 
					productEntityRepository.findByOwnerStore(storePag.getCondition(), PageRequest.of(storePag.getIndex()-1, storePag.getSize(), Sort.by(new Sort.Order(Direction.ASC, "id")))));
			return d;
		}else {
			return null;
		}
	}
	@Override
	public ProductEntity changeProduct(ProductEntity product) {
		if(product!=null) {
			return productEntityRepository.saveAndFlush(product);
		}else {
			return null;
		}
	}
	@Override
	public Boolean deleteProduct(ProductForm product) {
		if(product!=null) {
			productEntityRepository.deleteById(product.getCondition());
			ProductEntity p = productEntityRepository.findById(product.getCondition()).get();
			if(p!=null) {
				return false;
			}else {
				return true;
			}
		}else {
			return false;
		}
	}
	@Override
	public ProductEntity updateOrgProduct(ProductEntity product) {
		if(product!=null) {
			ProductEntity pro = productEntityRepository.findById(product.getId()).orElse(null);
			if(pro!=null) {
				pro.setProductName(product.getProductName());
				pro.setProductImage(product.getProductImage());
				pro.setProductDescription(product.getProductDescription());
				pro.setProductPrice(product.getProductPrice());
				return productEntityRepository.saveAndFlush(pro);
			}else {
				return null;
			}
			
		}else {
			return null;
		}
	}

	

}
